<template>
	<view>
		<ul>
			<li @click="handleAdd">
				<img src="/static/OrderRecord.png" class="order" alt="">
				<p>添加习题</p>
				<img src="/static/Arrow.png" class="arrow" alt="">
			</li>
			<li @click="handleEdit">
				<img src="/static/OrderRecord.png" class="order" alt="">
				<p>题库管理</p>
				<img src="/static/Arrow.png" class="arrow" alt="">
			</li>
			<li>
				<img src="/static/OrderRecord.png" class="order" alt="">
				<picker @change="bindPickerChange" :value="topicIndex" :range="array" style="width: 100%;">
					<view class="picker">
						<text>当前题库：{{array[topicIndex]}}</text>
					</view>
				</picker>

				<img src="/static/Arrow.png" class="arrow" alt="">
			</li>
			<li @click="handleImport">
				<img src="/static/OrderRecord.png" class="order" alt="">
				<p>导入题库</p>
				<img src="/static/Arrow.png" class="arrow" alt="">
			</li>
			<li @click="handleExport">
				<img src="/static/OrderRecord.png" class="order" alt="">
				<p>导出题库</p>
				<img src="/static/Arrow.png" class="arrow" alt="">
			</li>
			<li @click="clear">
				<img src="/static/OrderRecord.png" class="order" alt="">
				<p>清除本地题库</p>
				<img src="/static/Arrow.png" class="arrow" alt="">
			</li>
		</ul>

	</view>
</template>

<script>
	import uniCopy from '../../lib/js_sdk/xb-copy/uni-copy.js'
	export default {
		data() {
			return {
				array: ['默认题库', '缓存题库', '默认&缓存题库'],
				topicIndex: 0
			};
		},
		onShow() {
			this.topicIndex = Number(uni.getStorageSync('topicType'));
		},
		methods: {
			clear(){
				uni.showModal({
				    title: '提示',
				    content: '确认删除本地缓存的题库吗',
				    success: function (res) {
				        if (res.confirm) {
				            try {
				                uni.removeStorageSync('topicList');
								uni.showToast({
									title: '清除成功',
									icon: 'none'
								})
				            } catch (e) {
				                // error
				            }
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
				
			},
			handleExport() {
				let value = uni.getStorageSync('topicList') || '[]';
				uniCopy({
					content: value,
					success: (res) => {
						uni.showToast({
							title: '复制本地缓存题库成功',
							icon: 'none'
						})
					},
					error: (e) => {
						uni.showToast({
							title: e,
							icon: 'none',
							duration: 3000,
						})
					}
				})
			},
			handleImport() {
				uni.navigateTo({
					url: '/pages/importTopic/importTopic'
				})
			},
			handleAdd() {
				uni.navigateTo({
					url: '/pages/addTopic/addTopic'
				})
			},
			handleEdit() {
				uni.navigateTo({
					url: '/pages/editTopic/editTopic'
				})
			},
			handleChose() {

			},
			bindPickerChange(e) {
				console.log('picker发送选择改变，携带值为', e.detail.value)
				this.topicIndex = e.detail.value
				uni.setStorageSync('topicType', this.topicIndex);
			},

		}
	}
</script>

<style lang="less" scoped>
	ul {
		width: 100vw;
		margin-top: 30rpx;
		box-sizing: border-box;
		padding: 0 40rpx;

		li {
			overflow: hidden;
			height: 80rpx;
			border-bottom: 2rpx solid #efefef;
			display: flex;
			align-items: center;
			position: relative;

			p {
				font-size: 30rpx;
			}

			.number {
				color: #00e69d;
				position: absolute;
				right: 0;
			}

			.order {
				width: 35rpx;
				height: 40rpx;
				margin-right: 22rpx;
			}

			.tel {
				width: 40rpx;
				height: 40rpx;
				margin-right: 19rpx;
			}

			.arrow {
				width: 15rpx;
				height: 25rpx;
				position: absolute;
				top: 26rpx;
				right: 0;
			}
		}
	}
</style>
